<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"/>
    <title>HELLO，AMAP!</title>
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>

<div id="container"></div>
<div id="my-panel"></div>
</body>
</html>

<!-- 需要设置元素的宽高样式 -->
<script type="text/javascript">
    window._AMapSecurityConfig = {
        securityJsCode: "18126c12cf447573ea9216ae19829993",
    };
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=e3c0f3854e1abb83c1726ea0f94f0ac0"></script>
<script type="text/javascript">
    //地图初始化应该在地图容器 <div> 已经添加到 DOM 树之后
    const map = new AMap.Map("container", {
        viewMode: "2D", //默认使用 2D 模式
        zoom: 11, //地图级别
        center: [116.397428, 39.90923], //地图中心点
    });

    const startLngLat = [116.379028, 39.865042] //起始点坐标
    const endLngLat = [116.427281, 39.903719] //终点坐标

    function beginDriving(start, end) {
        const points = [{
                keyword: start
            }, //起始点坐标
            {
                keyword: end
            }
        ]

        //引入和创建驾车规划插件
        AMap.plugin(["AMap.Driving"], function() {
            const driving = new AMap.Driving({
                map: map,
                panel: "my-panel", //参数值为你页面定义容器的 id 值<div id="my-panel"></div>
                policy:AMap.DrivingPolicy.LEAST_TIME
            });
            //获取起终点规划线路
            driving.search(points, function(status, result) {
                if (status === "complete") {
                    //status：complete 表示查询成功，no_data 为查询无结果，error 代表查询错误
                    //查询成功时，result 即为对应的驾车导航信息
                    console.log(result);
                } else {
                    console.log("获取驾车数据失败：" + result);
                }
            });
        });
    }
</script>